package com.first.nell.info.face

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatImageView
import kotlin.math.max

class FaceResultImageView: AppCompatImageView {

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context!!,
        attrs,
        defStyleAttr,
    )

    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas) {

        val radius = max(width, height)/2

        // 创建一个背景画笔
        val backgroundPaint = Paint()
        backgroundPaint.color = Color.parseColor("#FFFFFF")

        val clipPath = Path()
        val rect = RectF(0f, 0f, width.toFloat(), height.toFloat())
        clipPath.addRoundRect(rect, radius.toFloat(), radius.toFloat(), Path.Direction.CW)

        // 绘制背景
        canvas.drawPath(clipPath, backgroundPaint)

        // 设置圆角裁剪
        canvas.clipPath(clipPath)

        super.onDraw(canvas)
    }
}